﻿$(function () {
    $('#btnSearch').click(function () {
        search();
    });

    $('#btnBack').click(function () {
        history.back();
    });

    $('#btnAdd').click(function () {
        edit();
    });

    initTable({ typeID: $("#hdnTypeID").val() });
});

//加载列表
function initTable(queryData, pageNumber) {
    $("#maingrid").ligerGrid({
        url: '/AdverseEvent/AdverseEventReason/Search?ram=' + Math.random(),
        parms: queryData,
        columns: [
        { display: '根因', name: 'Reason', align: 'left' },
        { display: '排序', name: 'Sort', width: 100 },
        {
            display: '操作', name: '', width: 200, render: function (row) {
                var html = '<a href="javascript:void(0)" class="a-edit" onclick="edit(' + row.ID + ')">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;';
                html += '<a href="javascript:void(0)" class="a-del" onclick="del(' + row.ID + ')">删除</a>';
                return html;
            }
        }],
        usePager: false,
        rownumbers: true,
        tree: {
            columnName: 'Reason',
            idField: 'ID',
            parentIDField: 'ParentID'
        },
        height: '100%',
        heightDiff: 25
    });
    GridNoData($("#maingrid"));
}

//搜索
function search() {
    $("#maingrid").ligerGrid().setParm("typeID", $("#hdnTypeID").val())
    $("#maingrid").ligerGrid().loadData();
}

//添加/编辑
function edit(id) {
    $.ligerDialog.open({
        height: 300,
        width: 450,
        title: '编辑根因',
        url: '/AdverseEvent/AdverseEventReason/Edit?id=' + id,
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: { typeID: $("#hdnTypeID").val() },
        buttons: [{
            text: '回调函数', onclick: function (i, d) {
                $("#btnSearch").click();
            }
        }]
    });
    $(".l-dialog-buttons").hide();
}

//删除
function del(id) {
    $.ligerDialog.confirm('确定删除？', function (value) {
        if (value) {
            $.ajax({
                url: "/AdverseEvent/AdverseEventReason/Delete",
                type: "POST",
                cache: false,
                data: { id: id },
                success: function (data) {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        $.ligerDialog.success("删除成功");
                        $('#btnSearch').click();
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                },
                error: function () {
                    $.ligerDialog.error("程序错误");
                }
            });
        }
    });
}